package com.pkq.CAS;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author ピカチュウ(皮卡啾)
 * @date 2021/9/6 21:15
 * @Description: CAS是什么？比较并交换。compareAndSet
 */
public class CASDemo {

    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(100);

        System.out.println("***********ABA 问题复现************");
        // ABA 问题 模拟
        // 线程1，把主存中的 100 改变成 101， 然后又改变成100
        new Thread(() -> {
            atomicInteger.compareAndSet(100, 101);
            System.out.println("工作时间 1s");
            System.out.println("线程1：把100 改成101");
            atomicInteger.compareAndSet(101, 100);
            System.out.println("线程1：把101 又改成100");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1").start();

        // 线程2 去比较主存中的值，还是 100， 但是现在的100已经不是原来的100了，经典的狸猫换太子问题
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("工作时间 5s");
            boolean flag = atomicInteger.weakCompareAndSet(1000, 2019);
            System.out.println(flag + " 线程2 " + atomicInteger.get());
            if (flag) {
                System.out.println("线程2：成功把100 改成 2019");
            }
        }, "t1").start();


        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        // 解决 ABA 问题
        System.out.println("***********解决 ABA 问题************");
        AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100, 1);

        new Thread(() -> {
            System.out.println("t1拿到的初始版本号:" + atomicStampedReference.getStamp());

            //睡眠1秒，是为了让t2线程也拿到同样的初始版本号
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            atomicStampedReference.compareAndSet(100, 101, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
            atomicStampedReference.compareAndSet(101, 100, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
        }, "t1").start();

        new Thread(() -> {
            int stamp = atomicStampedReference.getStamp();
            System.out.println("t2拿到的初始版本号:" + stamp);

            //睡眠3秒，是为了让t1线程完成ABA操作
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2在t1完成ABA操作后拿到最新的版本号:" + atomicStampedReference.getStamp());
            System.out.println(atomicStampedReference.compareAndSet(100, 2019, stamp, atomicStampedReference.getStamp() + 1) + "\t当前 值:" + atomicStampedReference.getReference());
        }, "t2").start();


    }
}

